//
//  CZvisitorView.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/9.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit

// MARK: - ******************* 定义代理 ********************
protocol CZvisitorViewDelegate: NSObjectProtocol {
    func registBtnDidClick ()
    func loginBtnDidClick ()
}
class CZvisitorView: UIView {

  
    //代理属性
    var delegate = CZvisitorViewDelegate?()
    
    
    //写个闭包
    var coulouse1 : (()->())?
    var coulouse2 : (()->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
        
        self.backgroundColor = UIColor(red: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1.0)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //iconview 实现转动
    func setrotation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置anim 属性
        anim.duration = 4
        anim.repeatCount = MAXFLOAT
        anim.toValue = M_PI*2
        //动画完成不要回复原来状态
        anim.removedOnCompletion = false
        
        //开始动画
        iconView.layer.addAnimation(anim, forKey: nil)
        
    
    
    }
    func setvisitor(title: String , imageName: String) {
    
       iconView.image = UIImage.init(named: imageName)
        messageLabel.text = title
        
        //隐藏房子
        
        homeView.hidden = true
        
        
        //隐藏遮盖
        coverView.hidden = true
        
    
    }
    
    
    
    
    //添加子控件
    func setUI () {
    
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //防止干扰自动布局
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
    
    //设置layout
    // 将约束添加到父控件身上
    /// 转轮
    self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
    
    /// 遮盖
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    /// 房子
    addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    
    /// 消息label
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    
    /// 当参照的view为nil时,attribute必须设置为NSLayoutAttribute.NotAnAttribute
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
    
    /// 注册按钮
    /// 阿拉伯语言
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    
    /// 登录按钮
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
}


// MARK: - ******************* view 按钮点击 ********************
    func registBntDidClick() {
     self.coulouse1!()
        
        
        //代理方法
        delegate?.registBtnDidClick()
    
    }
    
    func loginBntDidClick() {
        self.coulouse2!()
        //代理方法
        delegate?.loginBtnDidClick()

    }

  



   // MARK: - ******************* 懒加载 ********************
   
    
    lazy var iconView : UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    lazy var coverView : UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    lazy var homeView : UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_house"))
    lazy var messageLabel : UILabel = {
    let lab = UILabel()
        lab.text = "的方式和圣诞节,合适的凤凰花多少分环境奋斗就是峰会上的活雷锋分 发送到;雷锋号开始的恢复快收到货大家是否福建省的大家上来就分开交多少"
        lab.textAlignment = NSTextAlignment.Center
        lab.font = UIFont.systemFontOfSize(13)
        lab.numberOfLines = 0
        return lab
        
    }()
    lazy var registerButton : UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0), forState: .Normal)
        
        //注册按钮点击
        btn.addTarget(self, action: #selector(CZvisitorView.registBntDidClick), forControlEvents: .TouchUpInside)
        return btn
    
    }()
    lazy var loginButton : UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0), forState: .Normal)
        
        //登陆按钮点击
        btn.addTarget(self, action: #selector(CZvisitorView.loginBntDidClick), forControlEvents: .TouchUpInside)
        return btn
        
    }()

    

    
    
    
    
}
